// pages/meetingAdd/meetingAdd.js
import WxValidate from '../../utils/WxValidate.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.url,
    yuhuiIds: [],//与会人 id
    joinlist: [],//与会人
    date1: [
      "15分钟前",
      "30分钟前",
      "50分钟前"
    ]
  },
  //去选择与会人
  goYuhuiren: function (e) {
    wx.navigateTo({
      url: '../yuHuiRen/yuHuiRen?userIds=' + this.data.yuhuiIds.join(','),
    })
  },
  bindmeetRemind: function (e) {
    this.setData({
      index: e.detail.value
    })
    console.log(e);
  },
  //选择日期
  bindDateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      theday: e.detail.value
    })
    console.log(this.data.theday)
  },
  //选择时间
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      title: {
        required: true,
        // minlength: 2
      },
      mDate: {
        required: true
      },
      mTime: {
        required: true,
      },
      site: {
        required: true
      },
      mRemind: {
        required: true
      },
      basicFunction: {
        required: true
      }
    }
    const messages = {
      title: {
        required: '请输入会议主题'
      },
      mDate: {
        required: '请选择日期'
      },
      mTime: {
        required: '请选择时间',
      },
      site: {
        required: '请输入会议地点'
      },
      mRemind: {
        required: '请选择会议提醒时间'
      },
      basicFunction: {
        required: '请输入会议概要'
      }
    }
    this.WxValidate = new WxValidate(rules, messages);
  },
  // 表单提交
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var _this = this;
    var params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    } else if (this.data.joinlist.length < 1) {
      this.showModal({ msg: "请至少选择1位与会人" })
    } else {
      params['startTime'] = params.mDate + ' ' + params.mTime;
      var mRemind = params.mRemind;
      if (mRemind == 0) {
        mRemind = 10;
      } else if (mRemind == 1) {
        mRemind = 30;
      } else if (mRemind == 2) {
        mRemind = 50;
      }
      params['remind'] = mRemind;
      params['userIds'] = _this.data.yuhuiIds,
        console.log(params);
      wx.request({
        header: app.header(),
        url: app.globalData.url + '/meeting/addMeeting',//新增会议接口
        data: params,
        method: 'POST',
        success: function (res) {
          if (res.statusCode == 405) {
            app.getLogout();
            return;
          }
          if (res.data.status == 'success') {
            wx.showToast({
              title: '保存成功',//这里打印出登录成功
              icon: 'success',
              duration: 1000,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  wx.switchTab({
                    url: '../wode/wode'
                  })
                }, 2000) //延迟时间
              }
            })
          } else {
            _this.showModal({ msg: '提交失败' })
          }
        }
      })
    };
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.joinlist.join(','));
    this.initValidate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})